//
//  DetailAdd.swift
//  Speek
//
//  Created by MikeHo on 2023/6/29.
//

import SwiftUI

struct DetailAdd: View {
    // View Properties
    @State private var name: String = ""
    @State private var selectIncomeType: String = "工资"
    @State private var selectOutputType: String = "学习"
    @State private var date: Date = .init()
    @State private var cost: Double = 0.0
    @State private var tag: Bool = false
    @State private var incomeType = ["工资","奖学金","礼物","理财","其他"]
    @State private var outputType = ["学习","医疗","饮食","维修","旅行","运动","娱乐","购物","理财","其他"]
    
    // Emvironment Properties
    @Environment(\.dismiss) private var dismiss
    @Environment(\.managedObjectContext) private var context
    
    var body: some View {
        NavigationStack{
            List{
                Section("交易类型"){
                   
                    Toggle(isOn: $tag, label: {
                        Text(tag ? "收入" : "支出")
                    })
                }
                Section("事件"){
                    TextField("账单事件",text:$name)
                }
                Section("日期"){
                    DatePicker("",selection: $date,displayedComponents: [.date])
                        .labelsHidden()
                }
                Section("金额"){
                    TextField(value:$cost,formatter: currencyFormatter){}
                        .labelsHidden()
                        .keyboardType(.decimalPad)
                }
                
                Section("种类"){
                    if tag{
                        //收入种类选择
                        Picker(selection: $selectIncomeType,
                               label: Image(selectIncomeType)
                            .padding(.leading,5)){
                                ForEach(incomeType,id:\.self){type in
                                    Text(type)
                                }
                            }
                        .pickerStyle(DefaultPickerStyle())
                    }else{
                        //支出种类选择
                        Picker(selection: $selectOutputType,
                               label: Image(selectOutputType)
                            .padding(.leading,5)){
                                ForEach(outputType,id:\.self){type in
                                    Text(type)
                                }
                            }
                        .pickerStyle(DefaultPickerStyle())
                    }
                }
            }
            .preferredColorScheme(.dark)
            .navigationTitle("新建账单")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar{
                ToolbarItem(placement: .navigationBarTrailing){
                    Button("添加", action: AddBill)
                }
                ToolbarItem(placement: .navigationBarLeading){
                    Button("取消"){
                        dismiss()
                    }
                }
            }
            
        }
        
    }
    
    // Adding New Expense to Core Data
    func AddBill(){
        let testDate = Date()
        print(testDate)
        do{
            let newBill = BillDetail(context: context)
            newBill.name = name
            if tag {
                newBill.type = selectIncomeType
            }else{
                newBill.type = selectOutputType
            }
            newBill.date = date
            newBill.tag = tag
            newBill.cost = cost
            
            try context.save()
            //添加成功后关掉窗口
            dismiss()
        }catch{
            //Do Action
            print(error.localizedDescription)
        }
    }
}

//货币格式
let currencyFormatter: NumberFormatter={
    let formatter = NumberFormatter()
    formatter.allowsFloats = false
    formatter.numberStyle = .currency
    formatter.currencySymbol = "¥" // 设置人民币符号
    return formatter
}()
